//
//  RtViewController.m
//  PlayVideo
//
//  Created by myzc on 16/6/7.
//  Copyright © 2016年 myzc. All rights reserved.
//

#import "MyzcViewController.h"
#import "AVPViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#import "Model.h"
#import "MyzcTableViewCell.h"
#import "CourseViewController.h"
#import "AFHTTPSessionManager.h"
#import "UIKit+AFNetworking.h"

@interface MyzcViewController()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)NSMutableArray *modelArray;
//创建tableview
@property (nonatomic, strong)UITableView *tableview;

//@property (nonatomic, strong)NSString *id;
@end

@implementation MyzcViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"1.请选择课程";
//      self.view.backgroundColor=[UIColor whiteColor];
   _tableview = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]style:UITableViewStylePlain];
   _tableview.delegate = self;
   _tableview.dataSource = self;
    
    [self.view addSubview:_tableview];
    
    //设置navigationItem上的字体内容
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] init];
    backButtonItem.title = @"";
    self.navigationItem.backBarButtonItem = backButtonItem;
    
    
    self.modelArray = [NSMutableArray array];
    
//    [self.modelArray addObject:@"1"];
//    [self.modelArray addObject:@"2"];
//    [self.modelArray addObject:@"3"];

   
     // 创建AFHTTPSessionManager对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //上传json格式
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    //声明获取到的数据格式 AFN会json解析返回的数据   有时接口返回的数据不合格会报3840错误，大致是AFN无法解析返回来的数据
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    // get请求也可以直接将参数放在字典里，AFN会自己讲参数拼接在url的后面，不需要自己凭借
    //    NSDictionary *param = @{@"user_id":userId, @"sale_date":date, @"accessToken":@"e9c0e60318ebd07ec2fe", @"area_type":areaType};
    // 创建请求类
    
    [manager GET:@"http://114.55.115.175:8080/myzc98Server/courses"  parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        // 这里可以获取到目前数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功
      
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
//             NSLog(@"%@", dict);
       
         NSMutableDictionary  *dic = [dict objectForKey:@"data"];
         NSMutableArray *jsonArray = [dic objectForKey:@"list"];
        
        
        
        // 修改
        self.modelArray = [NSMutableArray array];
        
        for (NSDictionary *dic in jsonArray) {
            
            Model *model = [[Model alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelArray addObject:model];
        }
        
        
        [self.tableview reloadData];
        
        
        
        
                   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
     
        NSLog(@"%@", [error  localizedDescription]);
        
    }];


    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier= @"TableViewCell";
    MyzcTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
       cell = [[MyzcTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        if (indexPath.section == 0)
        {
            Model *mo = self.modelArray[indexPath.row];
            cell.textLabel.text = mo.title;
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头，距离右边有十几像素；

    }
    return cell;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return self.modelArray.count;
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
     Model *model = [_modelArray objectAtIndex:indexPath.row];

   
    CourseViewController *courseVC = [[CourseViewController alloc] init];
    
    //传值到第二个页面
    courseVC.model2 = model;

    [self.navigationController pushViewController:courseVC animated:YES];
    
 
    }
//- (void)btAction:(UIButton *)bt
//{
//    CourseTableViewController *courseTvc  =[[CourseTableViewController alloc] init];
//    [self.navigationController pushViewController:courseTvc animated:YES];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
